﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using MockupDesigner.Items;

namespace MockupDesigner.AppItems
{
    // Represents a selectable item in the Toolbox/>.
    public class ToolboxItem : ContentControl
    {
        // caches the start point of the drag operation
        private Point? dragStartPoint = null;



        public bool IsCustom
        {
            get { return (bool)GetValue(IsCustomProperty); }
            set { SetValue(IsCustomProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsCustom.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsCustomProperty =
            DependencyProperty.Register("IsCustom", typeof(bool), typeof(ToolboxItem), new UIPropertyMetadata(false));


        static ToolboxItem()
        {
            // set the key to reference the style for this control
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(ToolboxItem), new FrameworkPropertyMetadata(typeof(ToolboxItem)));
        }


        public static RoutedCommand Delete = new RoutedCommand();

        public ToolboxItem()
        {
            this.CommandBindings.Add(new CommandBinding(Delete, Delete_Executed));
        }

        private void Delete_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ((CustomToolbox)((ToolboxItem)sender).Parent).Items.Remove(sender);
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            this.dragStartPoint = new Point?(e.GetPosition(this));
        }

        private bool isDragDropping = false;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton != MouseButtonState.Pressed)
                this.dragStartPoint = null;

            if (this.dragStartPoint.HasValue && !isDragDropping)
            {
                // XamlWriter.Save() has limitations in exactly what is serialized,
                // see SDK documentation; short term solution only;
                string xamlString = XamlWriter.Save(this.Content);
                DragObject dataObject = new DragObject();
                dataObject.Xaml = xamlString;
                WrapPanel panel = VisualTreeHelper.GetParent(this) as WrapPanel;
                if (panel != null)
                {
                    // desired size for DesignerCanvas is the stretched Toolbox item size
                    ItemBase c = this.Content as ItemBase;
                    dataObject.DesiredSize = c != null ? c.GetPreferredSize() : new Size(panel.ItemWidth, panel.ItemHeight);
                    //dataObject.DesiredSize = new Size(panel.ItemWidth * scale, panel.ItemHeight * scale);
                }
                isDragDropping = true;
                DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);
                isDragDropping = false;
                e.Handled = true;
            }
        }
    }

    // Wraps info of the dragged object into a class
    public class DragObject
    {
        // Xaml string that represents the serialized content
        public String Xaml { get; set; }

        // Defines width and height of the DesignerItem
        // when this DragObject is dropped on the DesignerCanvas
        public Size? DesiredSize { get; set; }

    }
}
